﻿Imports TransportadoraToledoDAL
Public Class AbastecimentoBLL
    Public Sub New()

    End Sub
    Dim objDAL As AbastecimentoDAL
#Region "Propriedades"
    Private vei_codigo As Integer
    Public Property pVei_codigo() As Integer
        Get
            Return vei_codigo
        End Get
        Set(ByVal value As Integer)
            vei_codigo = value
        End Set
    End Property
    Private aba_km As Integer
    Public Property pAba_km() As Integer
        Get
            Return aba_km
        End Get
        Set(ByVal value As Integer)
            aba_km = value
        End Set
    End Property
    Private aba_valor As Double
    Public Property pAba_valor() As Double
        Get
            Return aba_valor
        End Get
        Set(ByVal value As Double)
            aba_valor = value
        End Set
    End Property
    Private aba_data As Date
    Public Property pAba_data() As Date
        Get
            Return aba_data
        End Get
        Set(ByVal value As Date)
            aba_data = value
        End Set
    End Property
    Private aba_codigo As Integer
    Public Property pAba_codigo() As Integer
        Get
            Return aba_codigo
        End Get
        Set(ByVal value As Integer)
            aba_codigo = value
        End Set
    End Property
#End Region

#Region "Métodos"
    Public Function inserir() As Boolean
        Dim ret As Boolean
        Try
            If (Me.aba_data = Nothing) Then
                Throw New Exception("Informe a data do abastecimento!")
            ElseIf (Me.aba_km = 0) Then
                Throw New Exception("Informe a kilometragem atual do veiculo")
            ElseIf (Me.aba_valor = 0) Then
                Throw New Exception("Informe o valor do abastecimento")
            ElseIf (vei_codigo = 0) Then
                Throw New Exception("Informe o veiculo do abastecimento!")
            Else
                objDAL = New AbastecimentoDAL
                objDAL.inserir(Me.vei_codigo, Me.aba_km, Me.aba_valor, Me.aba_data)
                ret = True
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return ret
    End Function
    Public Function alterar() As Boolean
        Dim ret As Boolean
        Try
           If (Me.aba_km = 0) Then
                Throw New Exception("Informe a kilometragem atual do veiculo")
            ElseIf (Me.aba_valor = 0) Then
                Throw New Exception("Informe o valor do abastecimento")
            ElseIf (vei_codigo = 0) Then
                Throw New Exception("Informe o veiculo do abastecimento!")
            ElseIf (Me.aba_codigo = 0) Then
                Throw New Exception("Abastecimento não localizado!")
            Else
                objDAL = New AbastecimentoDAL
                objDAL.alterar(Me.vei_codigo, Me.aba_km, Me.aba_valor, Me.aba_data, Me.aba_codigo)
                ret = True
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return ret
    End Function
    Public Function excluir() As Boolean
        Dim ret As Boolean
        Try
            If (Me.aba_codigo = 0) Then
                Throw New Exception("Abastecimento não localizado")
            Else
                objDAL = New AbastecimentoDAL
                objDAL.excluir(Me.aba_codigo)
                ret = True
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return ret
    End Function
    'retorna todas as linhas pesquisadas da tabela
    Function localizar(ByVal nome As String) As DataTable
        Dim tab As DataTable = Nothing
        Try
            objDAL = New AbastecimentoDAL
            tab = objDAL.localizar(nome)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function


    'retorna todas as linhas pesquisadas da tabela
    Function localizarPrimeiroUltimo(ByVal nome As String) As Integer
        Dim tab As DataTable = Nothing
        Dim codigo As Integer = 0
        Try
            If (nome = "ultimo") Then
                objDAL = New AbastecimentoDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("aba_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("aba_codigo").ToString
                End If
            ElseIf (nome = "primeiro") Then
                objDAL = New AbastecimentoDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("aba_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("aba_codigo").ToString
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return codigo
    End Function

    Function localizarProxAnteriro(ByVal nome As String, ByVal codigo As Integer) As Integer
        Dim tab As DataTable = Nothing
        Dim CodigoP As Integer = 0
        Try
            objDAL = New AbastecimentoDAL
            tab = objDAL.localizarProxAnterior(nome, codigo)
            If (tab.Rows.Count > 0) Then
                CodigoP = tab.Rows(0)("aba_codigo").ToString
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return CodigoP
    End Function

    'localizar uma linha da tabela w preenche as propriedades
    Sub localizar(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New AbastecimentoDAL
            tab = objDAL.localizar(codigo)
            If (tab.Rows.Count > 0) Then
                Me.aba_codigo = tab.Rows(0)("aba_codigo").ToString
                Me.aba_data = tab.Rows(0)("aba_data").ToString
                Me.aba_km = tab.Rows(0)("aba_km").ToString
                Me.aba_valor = tab.Rows(0)("aba_valor").ToString
                Me.vei_codigo = tab.Rows(0)("vei_codigo").ToString
            Else
                limpar()
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Function localizarAbasVeiculo(codigo As Integer) As DataTable
        Dim tab As DataTable = Nothing
        Try
            objDAL = New AbastecimentoDAL
            tab = objDAL.localizarAbasVeiculo(codigo)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function
    Public Function localizar(ByVal dt_inicio As Date, ByVal dt_final As Date) As DataTable
        Dim tab As DataTable
        Try
            objDAL = New AbastecimentoDAL
            tab = objDAL.localizar(dt_inicio, dt_final)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function
    Public Sub limpar()
        Me.aba_codigo = 0
        Me.aba_data = Nothing
        Me.aba_km = 0
        Me.aba_valor = 0
        Me.vei_codigo = 0
    End Sub
#End Region
End Class
